package com.cqrcb.cloud.config;

import com.cqrcb.cloud.config.redis.constant.RedisTools;
import com.cqrcb.cloud.config.redis.template.RedisRepository;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Huhailong
 * @Description 监听消息
 * @Date 2021/3/10.
 */
@Slf4j
@Component
public class ListenerMessage implements StreamListener<String, MapRecord<String, String, String>> {

    @Autowired
    private RedisRepository redisRepository;

    @SneakyThrows
    @Override
    public void onMessage(MapRecord<String, String, String> entries) {
        log.info("接受到来自redis的消息");
        System.out.println("message id "+entries.getId());
        System.out.println("stream "+entries.getStream());
        Map<String, String> value = entries.getValue();
        System.out.println("value "+ value);
        System.out.println("body "+ value.get("name"));
//        new Thread(){
//            @SneakyThrows
//            @Override
//            public void run() {
//                Thread.sleep(10 * 1000);
                redisRepository.getRedisTemplate().opsForStream().acknowledge("group", entries);
//            }
//        }.start();
    }
    
}
